<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../../plugins/layui/css/layui.css" media="all">
</head>

<body>

  <table class="layui-hide" id="test" lay-filter="test"></table>

  <script type="text/html" id="toolbarDemo">
    <div class="layui-input-block" style="float: left; position: relative;">
      <input style="width: auto;" type="text" id="select_orderId" name="select_orderId" lay-verify="required" placeholder="请输入病人姓名" autocomplete="off" class="layui-input">
    </div>
    <button class="layui-btn" lay-submit="" id="searchBtn" data-type="getInfo" style="float: left;margin-left:10px">搜索</button>
  <div class="layui-btn-container">
  </div>
</script>

  <script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs" lay-event="edit" style="width:50px" >保存</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" style="width:50px">删除</a>
  </script>


  <script src="../../plugins/layui/layui.all.js" charset="utf-8"></script>
  <script src="../../plugins/jquery/jquery.min.js"></script>

  <script>
      var url = location.search;

      var object = {};
      var reg = /\?/;
      if (url.match(reg)) {
          //判断传入参数，以问号截取，问号后是参数
          var chars = url.split('?')[1];

          //再截&号
          var arr = chars.split('&');

          //获得截取后的数组为键值对字符串
          for (var i = 0; i < arr.length; i++) {

              //保守一点确定看是否为 name=value形式
              var num = arr[i].indexOf("=");

              if (num > 0) {
                  //拼接字符串
                  var name = arr[i].substring(0, num);
                  var value = arr[i].substr(num + 1);

                  //拼接对象，并转码
                  object[decodeURIComponent(name)] = decodeURIComponent(value);
              }
          }
      }
      console.log(object)

      //      $.ajax({
      //          url:"/treatment/view?docId="+obj.doctorId,
      //          data:{docId:obj.doctorId},
      //          async:true,
      //          type:"GET"
      //      })

      //window.location="/treatment/view?docId="+obj.doctorId;

  </script>
  <script>
    layui.use('table', function () {
      var table = layui.table;

      table.render({
        elem: '#test'
        ,url:"/treatment/pageHelper?docId="+object.doctorId
        , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
        , defaultToolbar: ['filter', 'exports', 'print', {
          title: '提示'
          , layEvent: 'LAYTABLE_TIPS'
          , icon: 'layui-icon-tips'
        }]
        , title: '用户数据表'
        , cols: [[
          { fixed: 'left',width:40 }
          , { type: 'numbers', title: '序号' }
          , { field: 'patName', title: '姓名', width: 120,edit: 'text' }
          , { field: 'patAge', title: '年龄', width: 120,edit: 'text' }
          , { field: 'patSex', title: '性别', width: 120,edit: 'text' }
          , { field: 'personCard', title: '身份信息', width: 120,edit: 'text' }
          , { field: 'docName', title: '医生姓名', width: 120,edit: 'text' }
          , { field: 'conName', title: '科室姓名', width: 120 ,edit: 'text'}
          , { field: 'createTime', title: '就诊时间', width: 160 ,}
          , { field: 'intro', title: '病情描述', width: 120,edit: 'text'}
          , { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 200 }
        ]]
        , page: true

      });

      //头工具栏事件
      table.on('toolbar(test)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case 'getCheckLength':
            var data = checkStatus.data;
            console.log(data)
            var ids = "";
            for(var i = 0 ;i<data.length;i++){
                ids = "pid="+data[i].id+"";
            }
            console.log(ids)
            layer.msg('选中了：' + data.length + ' 个');
            break;
          //自定义头工具栏右侧图标 - 提示
          case 'LAYTABLE_TIPS':
            layer.alert('这是工具栏右侧自定义的一个图标按钮');
            break;
        };
      });

      //监听行工具事件
      table.on('tool(test)', function (obj) {
        var datas = obj.data;//获取行元素json
        if (obj.event === 'del') {
           layer.confirm('真的删除行么', function (index) {
               $.ajax({
                   url:"/treatment/del",
                   data:{id : datas.id},
                   async:true,
                   cache:false,
                   type:"POST",
                   success:function (result) {
                       console.log(result)
                       if(result.message == "删除成功"){
                           layer.msg(result.message);
                           window.location.reload();
                       }
                   }

               })
                layer.close(index);

            });

        } else if (obj.event === 'edit') {
          layer.confirm('确认保存',function (index) {
              $.ajax({
                  url:"/treatment/updateList",
                  data:{id:datas.id,patName:datas.patName,patAge:datas.patAge,patSex:datas.patSex,patPhone:datas.patPhone,docName:datas.docName,conName:datas.conName,intro:datas.intro,doctorId:object.doctorId},
                  async:true,
                  cache:false,
                  type:"POST",
                  success:function (result) {
                      if(result.message == "更新成功"){
                          layer.msg("数据更新成功")
                      }
                  }
              });
              layer.close(index);
          })
        }
      });
        $('#searchBtn').click(function () {
            var type = 'reload';
            active[type] ? active[type].call(this) : '';
        })
      var active = {
          reload:function () {
              var name = document.getElementById("select_orderId").value;

              console.log(name)
              if(true){
                  console.log(true);
                  table.reload('test',
                      {page:
                          {
                              curr: 1 //重新从第 1 页开始
                          }
                          , where: {patName:name,docId:object.doctorId}//这里传参  向后台
                          , url: "/treatment/selectList"
                          , method: 'get'
                      });

              }
              document.getElementById("select_orderId").innerHTML= name;

          }
      }
    });
  </script>

</body>

</html>